<template >
  <el-menu :default-openeds="opens" style="min-height: 100%; overflow-x: hidden"
           background-color="rgb(48, 65, 86)"
           text-color="#fff"
           active-text-color="#ffd04b"
           :collapse="isCollapsed"
           :collapse-transition="false"
           router
           class="el-menu-vertical-demo"
  >
    <div style="height: 60px; line-height: 60px; text-align: center">
      <img src="../assets/logo.png" alt="" style="width: 20px; position: relative; top: 5px; right: 5px">
      <b style="color: white" v-show="logoTextShow">后台管理系统</b>
    </div>
    <div class="toggle-button" @click="toggleCollapse" style="font-family: Rockwell;font-size: 14px"  >
      <el-icon><ArrowLeftBold /></el-icon>
      <span v-show="logoTextShow">
        折叠
      </span>
    </div>
    <div v-for="item in menus" :key="item.id">
      <div v-if="item.path">
        <el-menu-item :index="item.path">
          <el-icon>
            <component :is="item.icon"></component>
          </el-icon>
          <span v-show="logoTextShow">{{item.name}}</span>
        </el-menu-item>
      </div>
      <div v-else>
        <el-sub-menu :index="item.id+''">
          <template #title>
            <el-icon>
              <component :is="item.icon"></component>
            </el-icon>
            <span v-show="logoTextShow">{{item.name}}</span>
          </template>
          <div v-for="subItem in item.children" :key="subItem.id">
            <el-menu-item :index="subItem.path">
              <el-icon>
                <component :is="subItem.icon"></component>
              </el-icon>
              <span slot="title" v-show="logoTextShow">{{subItem.name}}</span>
            </el-menu-item>
          </div>
        </el-sub-menu>
      </div>
    </div>
  </el-menu>
</template>

<script>
import {UserFilled,Grid,House,Document,Expand,User,ArrowLeftBold } from "@element-plus/icons";

export default {
  name: "vaside",
  components: {
    Grid,
    UserFilled,
    House,
    Document,
    Expand,
    User,
    ArrowLeftBold
  },
  props: {

  },
  data(){
    return{
      isCollapsed: false,
      logoTextShow: true,
      menus:localStorage.getItem("menus")?JSON.parse(localStorage.getItem("menus")):[],

      //opens设置默认展开
      opens:localStorage.getItem("menus")?JSON.parse(localStorage.getItem("menus")).map(v=>v.id+''):[]
    }
  },
  methods:{
    toggleCollapse(){
      this.isCollapsed=!this.isCollapsed,
      this.logoTextShow=!this.logoTextShow,
      this.$emit('toggleCollapse',this.isCollapsed)
    }
  }
}
</script>

<style lang="less" scoped>
.toggle-button{
  // 添加背景颜色
  background-color:rgb(48, 65, 86);
  // 设置文本大小
  font-size:12px;
  // 设置文本行高
  line-height:50px;
  // 设置文本颜色
  color:#fff;
  //// 设置文本居中
  text-align: center;
  // 设置文本间距
  letter-spacing: 0.2em;
  // 设置鼠标悬浮变小手效果
  cursor:pointer;
}
</style>